/**
 * 时间日期相关操作
 */

export function addPreZero(num) {
    if (num < 10) {
        return `0${num}`;
    }
    return num;
}

/**
 * 获取本周、本月、全年
 */
export function getThisDate(type) {
    const now = new Date(); // 当前日期
    const nowDayOfWeek = now.getDay(); // 今天本周的第几天
    const nowDay = now.getDate(); // 当前日
    const nowMonth = now.getMonth(); // 当前月
    const nowYear = now.getFullYear(); // 当前年
    let reArr = [];
    if (type === 'week') {
        const weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
        const weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
        reArr = [weekStartDate, weekEndDate]
    } else if (type === 'month') {
        const monthStartDate = new Date(nowYear, nowMonth, 1);
        const monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowYear, nowMonth));
        reArr = [monthStartDate, monthEndDate]
    } else if (type === 'year') {
        const yearStartDate = new Date(nowYear, 0, 1);
        const yearEndDate = new Date(nowYear, 11, getMonthDays(nowYear, 11));
        reArr = [yearStartDate, yearEndDate]
    } else if (type === 'day') {
        reArr = [new Date(), new Date()]
    }
    return reArr
}

// 获得某月的天数
export function getMonthDays(nowYear, myMonth) {
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 可以指定日期和时间分隔符
 * @param datetime 国际化日期格式
 */
export function formatWithSeperator(datetime, dateSeprator, timeSeprator) {
    if (datetime != null) {
        const dateMat = new Date(datetime);
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const hh = addPreZero(dateMat.getHours());
        const mm = addPreZero(dateMat.getMinutes());
        const ss = addPreZero(dateMat.getSeconds());
        const timeFormat = `${year + dateSeprator + month + dateSeprator + day} ${hh}${timeSeprator}${mm}${timeSeprator}${ss}`;
        return timeFormat;
    }
    return null;
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018年09月23日 星期一 11:54:16
 * 可以指定日期和时间分隔符
 * @param datetime 国际化日期格式
 */
export function formatWithChar(datetime) {
    if (datetime != null) {
        const dateMat = new Date(datetime);
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const week = `星期${'日一二三四五六'.charAt(dateMat.getDay())}`;
        const hh = addPreZero(dateMat.getHours());
        const mm = addPreZero(dateMat.getMinutes());
        const ss = addPreZero(dateMat.getSeconds());
        const timeFormat = `${`${year}年${month}月${day}日`} ${week} ${hh}${':'}${mm}${':'}${ss}`;
        return timeFormat;
    }
    return null;
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成 2018/09/23 11:54:16
 * @param datetime 国际化日期格式
 */
export function format(datetime) {
    return formatWithSeperator(datetime, '/', ':');
}
/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成 2018/09/23 11:54:16
 * @param datetime 国际化日期格式
 */
export function formatT(datetime) {
    return formatWithSeperator(datetime, '-', ':');
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 可以指定日期和时间分隔符
 * @param datetime 国际化日期格式 返回年月日
 */
export function formatWithYear(datetime, dateSeprator = '/') {
    if (datetime != null) {
        const dateMat = new Date(datetime);
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const timeFormat = year + dateSeprator + (month < 10 ? `0${month}` : month) + dateSeprator + (day < 10 ? `0${day}` : day);
        return timeFormat;
    }
    return '';
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 可以指定日期和时间分隔符
 * @param datetime 国际化日期格式 返回年月
 */
export function formatWithMonth(datetime, dateSeprator = '/') {
    if (datetime != null) {
        const dateMat = new Date(datetime);
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const timeFormat = year + dateSeprator + month;
        return timeFormat;
    }
    return '';
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 计算当前日期的years年之前的日期
 * 可以指定日期和时间分隔符
 * @param years 年份变量
 */
export function formatPreYear(years, dateSeprator = '/') {
    if (years != null) {
        // 获取years年前得日期
        const date = new Date();
        const year = date.getFullYear() - years;
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const nowDate = year + dateSeprator + (month < 10 ? `0${month}` : month) + dateSeprator + (day < 10 ? `0${day}` : day);
        return nowDate;
    }
    return '';
}
/**
 * 时间格式化
 * 获取日期向前推进多少天
 */
export function getBeforeDate(e) {
    var n = e
    var d = new Date();
    var year = d.getFullYear()
    var mon = d.getMonth() + 1
    var day = d.getDate()
    if (day <= n) {
        if (mon > 1) {
            mon = mon - 1
        } else {
            year = year - 1
            mon = 12
        }
    }
    d.setDate(d.getDate() - n)
    year = d.getFullYear()
    mon = d.getMonth() + 1
    day = d.getDate()
    return new Date(year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day))
}

// 获取这周的周一
export function getFirstDayOfWeek(date) {
    let weekday = date.getDay() || 7
    date.setDate(date.getDate() - weekday + 1)
    return date
}
// 获取周最后一天
export function getFirstDayOnWeek(date) {
    let weekday = date.getDay()
    date.setDate(date.getDate() + (6 - weekday + 1))
    return date
}
// 获取本月最后一天
export function getFirstMonthOnWeek(date) {
    let currentMonth = date.getMonth()
    let nextMonth = ++currentMonth
    let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
    let oneDay = 1000 * 60 * 60 * 24
    return new Date(nextMonthFirstDay - oneDay)
}
